home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
OS
/
ZHandle.h
< prev
next >
Wrap
Text File
|
1997-06-20
|
5KB
|
154 lines
/*
* File: ZHandle.h
* Summary: A class to encapsulate relocatable memory.
* Written by: Jesse Jones
*
* Abstract: THandle uses reference counting so that when an
* THandle is copied the old and new objects point to
* the same block of memory. This means that THandle
* objects can be treated just like regular OSMemHandle's.
* In particular, they can be passed by value without
* allocating a new chunk of memory.
*
* Usage: There are three ways to allocate memory in Raven: operator new,
* THandle, and TPointer. The new operator should be used to
* allocate small amounts of memory that won't change size.
* THandle should be used for large chunks of memory. TPointer
* should be used for large chunks of memory that cannot move.
*
* When using THandle try to minimize the amount of
* time the handle is locked down: while it's locked the
* heap may be fragmented. If a memory allocation happens
* when the heap is fragmented it may fail.
*
* Copyright ゥ 1996 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <2> 6/20/97 JDJ Ctors use new SHandleOptions to work around ambiguity problems.
* <1> 1/14/96 JDJ Created
*/
#pragma once
#include <ZConstants.h>
#include <ZLockable.h>
#include <ZTypes.h>
//-----------------------------------
// Forward References
//
class ZHandleRef;
// ===================================================================================
// struct SHandleOptions
// Used to work around ambiguities in the ctors.
// ===================================================================================
struct SHandleOptions {
int options;
SHandleOptions() {options = kDontZeroBytes + kUseAppHeap;}
explicit SHandleOptions(int opt) {options = opt;}
};
// ===================================================================================
// class THandle
// ===================================================================================
class THandle : public MBaseLockable {
//-----------------------------------
// Initialization/Destruction
//
public:
virtual ~THandle();
explicit THandle(ulong bytes = 0, const SHandleOptions& options = SHandleOptions());
// The compiler will complain if you write code that looks like
// THandle(0, kUseTempHeap).
THandle(Handle takeHandle);
// Accepts responsibility for the Handle provided in takeHandle.
// If the handle is a Mac resource it is loaded (if neccesary)
// and then detached.
THandle(ResType type, ResID id, const SHandleOptions& options = SHandleOptions(), short fileRef = kNoFileRefNum);
// Uses ReadPartialResource to read the resource into the handle.
THandle(ResType type, const string& name, const SHandleOptions& options = SHandleOptions(), short fileRef = kNoFileRefNum);
THandle(const THandle& rhs);
THandle& operator=(const THandle& rhs);
//-----------------------------------
// API
//
public:
// ----- Conversion operators -----
operator Handle() const;
// ----- Access -----
const Byte* GetPtr() const;
Byte* GetPtr();
const Byte* GetUnsafePtr() const;
// The handle may not be locked so be careful!
Byte* GetUnsafePtr();
// ----- Sizing -----
ulong GetSize() const;
// Ignores the tail if present.
void SetSize(ulong bytes, bool zeroBytes = kDontZeroBytes);
// Note that this throws an exception if it fails. Also the
// handle should not be locked. If zeroBytes is set new bytes
// are zeroed instead of being uninitialized.
// ----- Debugging -----
void AddTail();
// Adds a 4-byte tail to the end of the handle. This can be used
// to check that you're not writing past the end of the handle.
void CheckTail() const;
void RemoveTail();
// ----- Misc -----
bool CanPurge(Handle gzHandle) const;
ulong PurgeableBytes() const;
void Detach();
// If the letter is being shared this will create a new letter
// referenced only by 'this'.
bool operator==(const THandle& rhs) const;
bool operator!=(const THandle& rhs) const {return !this->operator==(rhs);}
//-----------------------------------
// Inherited API
//
public:
virtual void Lock(bool moveHigh = kDontMoveHigh);
virtual void Unlock();
virtual bool IsLocked() const;
//-----------------------------------
// Member data
//
protected:
ZHandleRef* mHandleRef;
};